﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Ruleweaver.Rules;

namespace Ruleweaver.Conditions
{
	public class Condition
	{
		public Condition(Rule rule, ConditionKey key)
		{
			Contract.Requires(rule != null);
			Contract.Requires(key != null);

			Rule = rule;
			Key = key;
		}

		public Condition(Rule rule, Type targetType) : this(rule, new ConditionKey(targetType))
		{}

		public Condition(Rule rule, Type targetType, string name) : this(rule, new ConditionKey(targetType, name))
		{}

		public Rule Rule { get; private set; }

		public ConditionKey Key { get; private set; }

		public LambdaExpression ToLambdaExpression()
		{
			return Rule.ToLambdaExpression(Key.TargetType);
		}
	}

	public class Condition<TTarget> : Condition
	{
		public Condition(Rule rule) : base(rule, typeof(TTarget))
		{}

		public Condition(Rule rule, string name) : base(rule, typeof(TTarget), name)
		{}

		public new Expression<Func<TTarget, bool>> ToLambdaExpression()
		{
			return Rule.ToLambdaExpression<TTarget>();
		}
	}
}